﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpExt.Util
{
    public static class ByteArrayDumper
    {
        /// <summary>
        /// Metodo di servizio per fare il dump del buffer.
        /// Da usare per il debug
        /// </summary>
        public static String ToString(byte[] buffer, int offset, int count)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; ++i)
            {
                byte b = buffer[offset + i];

                if (b == 0x00)
                {
                    sb.Append("[00]");
                }
                else if (b == 0x1C)
                {
                    sb.Append("[1C]");
                }
                else if (b == 0x1E)
                {
                    sb.Append("[1E]");
                }
                else if (b == 0x1F)
                {
                    sb.Append("[1F]");
                }
                else if (b < 32 || b > 126)
                {
                    sb.Append("{").Append(b.ToString("X2")).Append("}");
                }
                else
                {
                    sb.Append((char)b);
                }
            }
            return sb.ToString();
        }
    }
}
